﻿using Modbus.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace 智慧大棚
{
    public partial class Form1 : Form
    {


        // modbus通讯类
        ModbusMaster master;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM21";
            serialPort1.DataBits = 8;
            serialPort1.BaudRate = 9600;
            serialPort1.StopBits = System.IO.Ports.StopBits.One;
            serialPort1.Parity = System.IO.Ports.Parity.None;


            master = ModbusSerialMaster.CreateRtu(serialPort1);


            serialPort1.Open();
        }
        private void btn_startOrStop_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                timer1.Stop();
            }
            else
            {
                timer1.Start();
            }
        }

        private void btn_setInterval_Click(object sender, EventArgs e)
        {
            timer1.Interval = Convert.ToInt32(ipt_interval.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            string time = now.ToString("yyyy-MM-dd HH:mm:ss");


            double? tem = null;
            double? shidu = null;
            double? O2 = null;
            double? Fengsu = null;

            // 温度勾选
            if (checkBox1.Checked)
            {
                // 读保持寄存器   从站地址   
                ushort[] values = master.ReadHoldingRegisters(1, 0x0000, 1);
                tem = values[0] / 10f;// 温度除以10


                // 添加节点

                //chart1.Series[0].Points.AddXY(time,tem);
                AddChartPoint(chart1.Series[0].Points, time, tem);
            }

            // 温度
            if (checkBox2.Checked)
            {
                // 读保持寄存器   从站地址   
                ushort[] values = master.ReadHoldingRegisters(1, 0x0001, 1);
                shidu = values[0] / 10f;// 温度除以10


                // 添加节点

                //chart1.Series[0].Points.AddXY(time,tem);
                AddChartPoint(chart1.Series[0].Points, time, shidu);
            }
            // 氧气
            if (checkBox3.Checked)
            {
                // 读保持寄存器   从站地址   
                ushort[] values = master.ReadHoldingRegisters(1, 0x0002, 1);
                O2 = values[0] / 10f;// 温度除以10


                // 添加节点

                //chart1.Series[0].Points.AddXY(time,tem);
                AddChartPoint(chart1.Series[2].Points, time, O2);
            }
            // 风速
            if (checkBox4.Checked)
            {
                // 读保持寄存器   从站地址   
                ushort[] values = master.ReadHoldingRegisters(1, 0x0003, 1);
                Fengsu = values[0] ;// 


                // 添加节点

                //chart1.Series[0].Points.AddXY(time,tem);
                AddChartPoint(chart1.Series[3].Points, time, Fengsu);
            }


        }




        private void AddChartPoint(DataPointCollection dataPoints,string
             time,object value)
        {
            dataPoints.AddXY(time,value);

            if (dataPoints.Count >= 15)
            {
                dataPoints.RemoveAt(0);
            }
        }
    }
}
